#!/usr/bin/make -f
# debian/rules for TiMidity++ 2.12.x
#
# GNU copyright 1997 by Joey Hess.
# Copyright (c) 2003 by Henrique de Moraes Holschuh
# Published under the GNU GPL license
# Based on previous work by Martin Mitchell <martin@debian.org>
#
# $Id: rules,v 1.24 2004/10/01 15:18:25 hmh Exp $

export TMPPKG := $(CURDIR)/debian/tmp

# Autotools
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_BUILD_ARCH_OS ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)

# FOR AUTOCONF 2.52 AND NEWER ONLY
CONFFLAGS =
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  CONFFLAGS += --build $(DEB_HOST_GNU_TYPE)
else
  CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# DEB_BUILD_OPTIONS
DEBUGFLAGS=-g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEBUGFLAGS += -O0
else
        DEBUGFLAGS += -O2
endif

EXTRAFLAGS:=-D_FORTIFY_SOURCE=2 -Wformat -Wformat-security

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CONFFLAGS += --enable-debug
endif

# audio and interface
ifeq ($(DEB_BUILD_ARCH_OS), linux)
audio = default,oss,nas,esd,jack,vorbis,flac,speex,alsa
interface = ncurses,emacs,xaw,alsaseq
else
audio = default,oss,nas,esd,jack,vorbis,flac,speex
interface = ncurses,emacs,xaw
endif

MAKE_CFLAGS:=$(EXTRAFLAGS) -fno-strict-aliasing -Wall -pipe $(DEBUGFLAGS)

##
##  The rules
##

build: doconfigure build-stamp
build-stamp:
	dh_testdir
	$(MAKE) CFLAGS="$(MAKE_CFLAGS)"
	touch build-stamp

doconfigure: doconfigure-stamp
doconfigure-stamp:
	dh_testdir
	rm -f autoconf/config.sub autoconf/config.guess
	ln -s /usr/share/misc/config.sub autoconf/config.sub
	ln -s /usr/share/misc/config.guess autoconf/config.guess

	CFLAGS="-fno-strict-aliasing -Wall -pipe $(DEBUGFLAGS)"
	./configure $(CONFFLAGS) \
	 --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info \
	 --sysconfdir=/etc --with-default-path=/etc/timidity \
	 --with-lispdir=/usr/share/emacs/site-lisp/timidity-el \
	 --with-x \
	 --with-tcl-includes=/usr/include/tcl8.4 \
	 --with-tk-includes=/usr/include/tcl8.4 \
	 --enable-audio=$(audio) \
	 --with-default-output=default \
	 --enable-interface=$(interface) \
	 --enable-dynamic=slang,tcltk,vt100,xskin,gtk \
	 --enable-server --enable-network --enable-spectrogram --enable-wrd
	touch doconfigure-stamp

clean:	configure doconfigure
	dh_testdir
	dh_testroot
	-find -type f -perm +111 ! -name 'rules' -exec chmod 644 {} \;
	-xargs -t -r chmod +x < debian/executable.files
	rm -f build-stamp
	$(MAKE) clean
	$(MAKE) distclean
	rm -f common.makefile TiMidity-uj.ad TiMidity.ad
	rm -f interface/tclIndex
	rm -f doc/timidity.1 doc/timidity.cfg.5
	rm -f autoconf/config.sub autoconf/config.guess
	rm -rf autom4te.cache
	rm -f config.log Makefile
	rm -f doconfigure-stamp
	dh_clean

configure:
	sh ./autogen.sh

install:
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) DESTDIR=$(TMPPKG) install

	#XAW interface
	mkdir -p $(TMPPKG)/etc/X11/app-defaults
	install -m 644 TiMidity.ad $(TMPPKG)/etc/X11/app-defaults/TiMidity
	mkdir -p $(TMPPKG)/etc/X11/ja_JP.eucJP/app-defaults
	install -m 644 TiMidity-uj.ad $(TMPPKG)/etc/X11/ja_JP.eucJP/app-defaults/TiMidity
	mkdir -p $(TMPPKG)/etc/X11/ja_JP.UTF-8/app-defaults
	install -m 644 debian/TiMidity-uj_UTF8.ad $(TMPPKG)/etc/X11/ja_JP.UTF-8/app-defaults/TiMidity

	if [ -r debian/timidity.cfg ] ; then \
		mkdir -p $(TMPPKG)/etc/timidity && \
		install -m 644 debian/timidity.cfg $(TMPPKG)/etc/timidity ;\
	fi

	mkdir -p $(TMPPKG)/etc/default

	# install desktop file
	mkdir -p $(TMPPKG)/usr/share/applications
	install -m 644 debian/timidity-interfaces-extra.desktop $(TMPPKG)/usr/share/applications

	# install an icon for menu
	mkdir -p $(TMPPKG)/usr/share/pixmaps
	install -m 644 debian/timidity.xpm $(TMPPKG)/usr/share/pixmaps

	# install Japanese man files
	mkdir -p $(TMPPKG)/usr/share/man/ja/man1
	install -m 644 doc/ja_JP.eucJP/timidity.1 $(TMPPKG)/usr/share/man/ja/man1
	mkdir -p $(TMPPKG)/usr/share/man/ja/man5
	install -m 644 doc/ja_JP.eucJP/timidity.cfg.5 $(TMPPKG)/usr/share/man/ja/man5

	# fix permissions
	chmod a+x  $(TMPPKG)/usr/lib/timidity/tk*.tcl

	# Lintian and Linda overrides
	( cd debian && \
	for i in *.linda ; do [ -r "$$i" ] && { \
		mkdir -p "$${i%%.linda}/usr/share/linda/overrides" ;\
		install  -m 644 "$$i" "$${i%%.linda}/usr/share/linda/overrides/$${i%%.linda}" ;\
	} ; done ; true )
	( cd debian && \
	for i in *.lintian ; do [ -r "$$i" ] && { \
		mkdir -p "$${i%%.lintian}/usr/share/lintian/overrides" ;\
		install  -m 644 "$$i" "$${i%%.lintian}/usr/share/lintian/overrides/$${i%%.lintian}" ;\
	} ; done ; true )

	#dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installemacsen
	dh_installman
	dh_installchangelogs
	dh_installinit -R --name=timidity -i -- defaults 99 01
	install -m 644 debian/timidity.default $(TMPPKG)/etc/default/timidity
	dh_install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
#	dh_perl -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
#	dh_makeshlibs -a
#	dh_perl -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
